package com.twu.biblioteca;

import java.io.IOException;
import java.io.PrintStream;

public class Option {
    PrintStream printStream;
    Library library;
    UserInputReader userInputReader;
    BookReserver bookReserver;
    MovieCollection movieCollection;
    LibraryPatrons libraryPatrons;

    public Option(PrintStream printStream, Library library, UserInputReader userInputReader, BookReserver bookReserver, MovieCollection movieCollection, LibraryPatrons libraryPatrons){
        this.printStream = printStream;
        this.library = library;
        this.userInputReader = userInputReader;
        this.bookReserver = bookReserver;
        this.movieCollection = movieCollection;
        this.libraryPatrons = libraryPatrons;
    }

    public boolean isValid(String input) {
         if (libraryPatrons.getLoggedInUser() != null)
             return true;
        else if(input.equals("1") || input.equals("4") || input.equals("5")) {
             return true;
         }
         return false;
    }

    public enum Options{
        DISPLAY_BOOKS("1"), RESERVE_BOOK("2"), CHECK_MEMBERSHIP("3"), DISPLAY_MOVIE("4"), LOG_IN("5"), INVALIDATE("X");
        private String intValue;

        private Options(String intValue) {
            this.intValue = intValue;
        }


        public String getIntValue() {
            return intValue;
        }
    }

    public void validateOption(String input) throws IOException {
        Options selectedOption = Options.INVALIDATE;
        Options[] values = Options.values();
        for (Options value : values) {
            if (input.equals(value.getIntValue()) && isValid(input))
                selectedOption = value;
        }
        switch(selectedOption) {
            case DISPLAY_BOOKS:
                library.displayAllBooks();
                break;
            case RESERVE_BOOK:
                bookReserver.reserveBook();
                break;
            case CHECK_MEMBERSHIP:
                libraryPatrons.printDetailsOrSeeLibrarian();
                break;
            case DISPLAY_MOVIE:
                movieCollection.displayAllMovieTitles();
                printStream.println("Enter the title of a movie:");
                Movie movie = movieCollection.findMovie(userInputReader.readUserInput());
                movieCollection.displayMovieInfo(movie);
                break;
            case LOG_IN:
                libraryPatrons.loginPrompt();
                validateOption(libraryPatrons.returnChosenOptionFromFullMenu());
                break;
            case INVALIDATE:
                printStream.println("Select a valid option!");
                validateOption(userInputReader.readUserInput());
                break;
        }
    }
}
